﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;

namespace LOPA
{
    public partial class EventFrequence : UserControl
    {
        public EventFrequence()
        {
            InitializeComponent();
            _bindEvent.DataSource = Program.Workspace.LopaProject.CauseEventList;
            dgvEventFrequence.DataSource = _bindEvent;
        }

        private void dgvEventFrequence_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvEventFrequence.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvEventFrequence.RowHeadersDefaultCellStyle.Font, rectangle,
                dgvEventFrequence.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void btnEdit_Click(object sender, EventArgs e){
            dgvEventFrequence.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e){
            dgvEventFrequence.Enabled = false;
            Program.Workspace.SaveProject(MainForm.mainForm.SaveFilePath);
            MessageBox.Show("引发事件频率保存成功!");
        }

        public void SaveWithoutRemider()
        {
            dgvEventFrequence.Enabled = false;
        }

        private void addNewRow_Click(object sender, EventArgs e)
        {
            if (!dgvEventFrequence.Enabled)
            {
                return;
            }
            CauseEventFrequenceItem item = new CauseEventFrequenceItem();
            _bindEvent.Insert(dgvEventFrequence.Rows.Count, item);
            dgvEventFrequence.Rows[dgvEventFrequence.Rows.Count - 1].Selected = true;
            dgvEventFrequence.CurrentCell = dgvEventFrequence[1, dgvEventFrequence.Rows.Count - 1];
        }

        private void defaultToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (new WarringForm("确认将恢复默认设置？").ShowDialog() == DialogResult.OK){
                Program.Workspace.LopaProject.CauseEventList = Program.Workspace.LopaProject.LoadEventDefaultData();
                _bindEvent.DataSource = Program.Workspace.LopaProject.CauseEventList;
            }
        }

    }
}
